# Run preamble
source("code/preamble.R")

# Load data
study2_data <- readRDS("data/study2_data.rds")

# Drop point-estimate condition
study2_data <- study2_data %>%
    subset(condition != "point_no_incen") %>%
    mutate(
        condition = condition %>%
            as.character() %>%
            factor(
                levels = c(
                    "dist_no_incen",
                    "dist_incen"
                )
            )
    )

# Reshape mean data
incen_mean_data <- study2_data %>%
    subset(grepl("dist", condition)) %>%
    select(pid2, policy, dem_mean, rep_mean, condition, admin_error_corrected) %>%
    pivot_longer(
        cols = matches("_mean"),
        names_pattern = "(.*)_mean",
        names_to = "which_partys_dist",
        values_to = "mean"
    ) %>%
    na.omit() %>%
    mutate(pid2 = case_when(
        pid2 == "Democrat" ~ "Among Democrats",
        pid2 == "Republican" ~ "Among Republicans"
    ))

# Reshape SD data
incen_sd_data <- study2_data %>%
    subset(grepl("dist", condition)) %>%
    select(pid2, policy, dem_sd, rep_sd, condition, admin_error_corrected) %>%
    pivot_longer(
        cols = matches("_sd"),
        names_pattern = "(.*)_sd",
        names_to = "which_partys_dist",
        values_to = "sd"
    ) %>%
    na.omit() %>%
    mutate(pid2 = case_when(
        pid2 == "Democrat" ~ "Among Democrats",
        pid2 == "Republican" ~ "Among Republicans"
    ))

# Run models
incen_mean_model <- lm_robust(
    mean ~ condition * policy * pid2 * which_partys_dist,
    data = incen_mean_data
)

incen_sd_model <- lm_robust(
    sd ~ condition * policy * pid2 * which_partys_dist,
    data = incen_sd_data
)

# Get group-average marginal effects
incen_mean_effects <- incen_mean_model %>%
    avg_slopes(
        variables = "condition",
        by = c("policy", "pid2", "which_partys_dist")
    )

incen_sd_effects <- incen_sd_model %>%
    avg_slopes(
        variables = "condition",
        by = c("policy", "pid2", "which_partys_dist")
    )

# Tables
incen_mean_effects %>%
    select(policy, pid2, which_partys_dist, estimate, std.error) %>%
    mutate( # Clean up which_partys_dist variable
        which_partys_dist = case_when(
            which_partys_dist == "dem" ~ "Democrat",
            which_partys_dist == "rep" ~ "Republican"
        )
    ) %>%
    mutate( # Clean up pid2 variable
        pid2 = pid2 %>% str_remove("Among ") %>% str_remove("s$")
    ) %>%
    rename( # Rename variables
        `Policy Issue` = policy,
        `Participant's Party` = pid2,
        `Which Party's Perceived Distribution?` = which_partys_dist,
        Estimate = estimate,
        SE = std.error
    ) %>%
    datasummary_df(
        title = "Group-Average Marginal Effects of Monetary Incentives on
        Perceived-Distribution Means",
        notes = "Note: Estimates come from a fully moderated model with the following
        predictors: whether incentives were offered, policy issue,
        participant's partisanship, and perceived-distribution subject. This
        table includes data from Study 2. n = 1,887.",
        output = "tables/incentive_effects_on_dist_means.txt"
    )

incen_sd_effects %>%
    select(policy, pid2, which_partys_dist, estimate, std.error) %>%
    mutate( # Clean up which_partys_dist variable
        which_partys_dist = case_when(
            which_partys_dist == "dem" ~ "Democrat",
            which_partys_dist == "rep" ~ "Republican"
        )
    ) %>%
    mutate( # Clean up pid2 variable
        pid2 = pid2 %>% str_remove("Among ") %>% str_remove("s$")
    ) %>%
    rename( # Rename variables
        `Policy Issue` = policy,
        `Participant's Party` = pid2,
        `Which Party's Perceived Distribution?` = which_partys_dist,
        Estimate = estimate,
        SE = std.error
    ) %>%
    datasummary_df(
        title = "Group-Average Marginal Effects of Monetary Incentives on
        Perceived-Distribution SDs",
        notes = "Note: Estimates come from a fully moderated model with the following
        predictors: whether incentives were offered, policy issue,
        participant's partisanship, and perceived-distribution subject. This
        table includes data from Study 2. n = 1,887.",
        output = "tables/incentive_effects_on_dist_sds.txt"
    )

# Print disclaimer
cat(
    "\n\n",
    " See incentive_effects_on_dist_means.txt for regressions of perceived-distribution MEANs on monetary incentives (i.e., Supplementary Table 7)."
)

########################
### ROBUSTNESS CHECK ###
########################

# Re-run models
incen_mean_error_model <- lm_robust(
    mean ~ condition * policy * pid2 * which_partys_dist,
    data = incen_mean_data %>% subset(admin_error_corrected == TRUE)
)

incen_sd_error_model <- lm_robust(
    sd ~ condition * policy * pid2 * which_partys_dist,
    data = incen_sd_data %>% subset(admin_error_corrected == TRUE)
)

# Get group-average marginal effects
incen_mean_error_effects <- incen_mean_error_model %>%
    avg_slopes(
        variables = "condition",
        by = c("policy", "pid2", "which_partys_dist")
    )

incen_sd_error_effects <- incen_sd_error_model %>%
    avg_slopes(
        variables = "condition",
        by = c("policy", "pid2", "which_partys_dist")
    )

# Tables
incen_mean_error_effects %>%
    select(policy, pid2, which_partys_dist, estimate, std.error) %>%
    mutate( # Clean up which_partys_dist variable
        which_partys_dist = case_when(
            which_partys_dist == "dem" ~ "Democrat",
            which_partys_dist == "rep" ~ "Republican"
        )
    ) %>%
    mutate( # Clean up pid2 variable
        pid2 = pid2 %>% str_remove("Among ") %>% str_remove("s$")
    ) %>%
    rename( # Rename variables
        `Policy Issue` = policy,
        `Participant's Party` = pid2,
        `Which Party's Perceived Distribution?` = which_partys_dist,
        Estimate = estimate,
        SE = std.error
    ) %>%
    datasummary_df(
        title = "Group-Average Marginal Effects of Monetary Incentives on
        Perceived-Distribution Means (Late Participants)",
        notes = "Note: Estimates come from a fully moderated model with the following
        predictors: whether incentives were offered, policy issue,
        participant's partisanship, and perceived-distribution subject. This
        table includes data from Study 2. n = 927.",
        output = "tables/late_participants_incentive_effects_on_dist_means.txt"
    )

incen_sd_error_effects %>%
    select(policy, pid2, which_partys_dist, estimate, std.error) %>%
    mutate( # Clean up which_partys_dist variable
        which_partys_dist = case_when(
            which_partys_dist == "dem" ~ "Democrat",
            which_partys_dist == "rep" ~ "Republican"
        )
    ) %>%
    mutate( # Clean up pid2 variable
        pid2 = pid2 %>% str_remove("Among ") %>% str_remove("s$")
    ) %>%
    rename( # Rename variables
        `Policy Issue` = policy,
        `Participant's Party` = pid2,
        `Which Party's Perceived Distribution?` = which_partys_dist,
        Estimate = estimate,
        SE = std.error
    ) %>%
    datasummary_df(
        title = "Group-Average Marginal Effects of Monetary Incentives on
        Perceived-Distribution SDs (Late Participants)",
        notes = "Note: Estimates come from a fully moderated model with the following
        predictors: whether incentives were offered, policy issue,
        participant's partisanship, and perceived-distribution subject. This
        table includes data from Study 2. n = 927.",
        output = "tables/late_participants_incentive_effects_on_dist_sds.txt"
    )
